home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectShow / Common / seekutil.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  4.4 KB  |  163 lines

  1. //------------------------------------------------------------------------------
  2. // File: SeekUtil.cpp
  3. //
  4. // Desc: DirectShow sample code - utility functions.
  5. //
  6. // Copyright (c) 2000-2001 Microsoft Corporation.  All rights reserved.
  7. //------------------------------------------------------------------------------
  8.  
  9.  
  10. #include "stdafx.h"
  11. #include <dshow.h>
  12.  
  13. //
  14. // Constants
  15. //
  16. const int TICKLEN=100, TIMERID=55;
  17.  
  18. //
  19. // Global data
  20. //
  21. static REFERENCE_TIME g_rtTotalTime=0;
  22. static UINT_PTR g_wTimerID=0;
  23. static HWND g_hwnd=0;
  24.  
  25. HRESULT ConfigureSeekbar(IMediaSeeking *pMS, CSliderCtrl Seekbar, CStatic& strPosition);
  26. void StartSeekTimer();
  27. void StopSeekTimer();
  28. void UpdatePosition(IMediaSeeking *pMS, REFERENCE_TIME rtNow, CStatic& strPosition);
  29. void ReadMediaPosition(IMediaSeeking *pMS, CSliderCtrl& Seekbar, CStatic& strPosition);
  30. void HandleTrackbar(IMediaControl *pMC, IMediaSeeking *pMS, 
  31.                     CSliderCtrl& Seekbar, CStatic& strPosition, WPARAM wReq);
  32.  
  33.  
  34. HRESULT ConfigureSeekbar(IMediaSeeking *pMS, CSliderCtrl Seekbar, CStatic& strPosition, HWND hwndOwner)
  35. {
  36.     HRESULT hr;
  37.  
  38.     // Disable seekbar for new file and reset tracker/position label
  39.     Seekbar.SetPos(0);
  40.     Seekbar.EnableWindow(FALSE);
  41.     g_rtTotalTime=0;
  42.  
  43.     strPosition.SetWindowText(TEXT("Position: 00m:00s\0"));
  44.  
  45.     DWORD dwSeekCaps = AM_SEEKING_CanSeekAbsolute | AM_SEEKING_CanGetDuration;
  46.  
  47.     // Can we seek this file?  If so, enable trackbar.
  48.     if (pMS && (S_OK == pMS->CheckCapabilities(&dwSeekCaps))) 
  49.     {
  50.         hr = pMS->GetDuration(&g_rtTotalTime);
  51.         Seekbar.EnableWindow(TRUE);
  52.     }
  53.  
  54.     g_hwnd = hwndOwner;
  55.  
  56.     return hr;
  57. }
  58.  
  59. void StartSeekTimer() 
  60. {
  61.     // Cancel any pending timer event
  62.     StopSeekTimer();
  63.  
  64.     // Create a new timer
  65.     g_wTimerID = SetTimer(g_hwnd, TIMERID, TICKLEN, NULL);
  66. }
  67.  
  68. void StopSeekTimer() 
  69. {
  70.     // Cancel the timer
  71.     if(g_wTimerID)        
  72.     {                
  73.         KillTimer(g_hwnd, g_wTimerID);
  74.         g_wTimerID = 0;
  75.     }
  76. }
  77.  
  78.  
  79. void ReadMediaPosition(IMediaSeeking *pMS, CSliderCtrl& Seekbar, CStatic& strPosition)
  80. {
  81.     HRESULT hr;
  82.     REFERENCE_TIME rtNow;
  83.  
  84.     // Read the current stream position
  85.     hr = pMS->GetCurrentPosition(&rtNow);
  86.     if (FAILED(hr))
  87.         return;
  88.  
  89.     // Convert position into a percentage value and update slider position
  90.     long lTick = (long)((rtNow * 100) / g_rtTotalTime);
  91.     Seekbar.SetPos(lTick);
  92.     
  93.     // Update the 'current position' string on the main dialog
  94.     UpdatePosition(pMS, rtNow, strPosition);
  95. }
  96.  
  97.  
  98. void UpdatePosition(IMediaSeeking *pMS, REFERENCE_TIME rtNow, CStatic& strPosition) 
  99. {
  100.     HRESULT hr;
  101.  
  102.     // If no reference time was passed in, read the current position
  103.     if (rtNow == 0)
  104.     {
  105.         // Read the current stream position
  106.         hr = pMS->GetCurrentPosition(&rtNow);
  107.         if (FAILED(hr))
  108.             return;
  109.     }
  110.  
  111.     // Convert the LONGLONG duration into human-readable format
  112.     unsigned long nTotalMS = (unsigned long) rtNow / 10000; // 100ns -> ms
  113.     int nSeconds = nTotalMS / 1000;
  114.     int nMinutes = nSeconds / 60;
  115.     nSeconds %= 60;
  116.  
  117.     // Update the display
  118.     TCHAR szPosition[24];
  119.     wsprintf(szPosition, _T("Position: %02dm:%02ds\0"), nMinutes, nSeconds);
  120.     strPosition.SetWindowText(szPosition);
  121. }
  122.  
  123.  
  124. void HandleTrackbar(IMediaControl *pMC, IMediaSeeking *pMS, 
  125.                     CSliderCtrl& Seekbar, CStatic& strPosition, WPARAM wReq)
  126. {
  127.     HRESULT hr;
  128.     static OAFilterState state;
  129.     static BOOL bStartOfScroll = TRUE;
  130.  
  131.     // If the file is not seekable, the trackbar is disabled. 
  132.     DWORD dwPosition = Seekbar.GetPos();
  133.  
  134.     // Pause when the scroll action begins.
  135.     if (bStartOfScroll) 
  136.     {       
  137.         hr = pMC->GetState(10, &state);
  138.         bStartOfScroll = FALSE;
  139.         hr = pMC->Pause();
  140.     }
  141.     
  142.     // Update the position continuously.
  143.     REFERENCE_TIME rtNew = (g_rtTotalTime * dwPosition) / 100;
  144.  
  145.     hr = pMS->SetPositions(&rtNew, AM_SEEKING_AbsolutePositioning,
  146.                            NULL,   AM_SEEKING_NoPositioning);
  147.  
  148.     // Restore the state at the end.
  149.     if (wReq == TB_ENDTRACK)
  150.     {
  151.         if (state == State_Stopped)
  152.             hr = pMC->Stop();
  153.         else if (state == State_Running) 
  154.             hr = pMC->Run();
  155.  
  156.         bStartOfScroll = TRUE;
  157.     }
  158.  
  159.     // Update the 'current position' string on the main dialog.
  160.     UpdatePosition(pMS, rtNew, strPosition);
  161. }
  162.  
  163.